﻿using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditCustomDashboardGadgetPresenter : BasePresenter
    {
        private readonly IEditCustomDashboardGadgetView _view;
        public IDiskAccess DiskAccess;
        protected Administrator _administrator;

        public EditCustomDashboardGadgetPresenter(IEditCustomDashboardGadgetView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }
        
        public EditCustomDashboardGadgetPresenter(IEditCustomDashboardGadgetView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditCustomDashboardGadget)
                _view.RedirectToPermissionDeniedPage();
        }

        public virtual void Initialize()
        {
            if (_view.GadgetID == -1)
            {
            }
            else
            {
                DashboardGadget gadget = SystemRepository.GetDashboardGadget(_view.GadgetID);
                _view.Filename = gadget.Filename.Substring(0, gadget.Filename.Length - 5); // Remove ".ascx"
                _view.Xml = gadget.GetXml();
                _view.Code = gadget.GetCode();
            }
        }
        
        public void UpdateHeading()
        {
            _view.HeadingText = _view.GadgetID > 0
                                    ? GetText(_view, "EditGadgetHeading") + " &mdash; " + _view.Filename
                                    : GetText(_view, "NewGadgetHeading");
        }

        public void RefreshProperties()
        {
            if (_view.GadgetID < 0)
                return;
            DashboardGadget gadget = SystemRepository.GetDashboardGadget(_view.GadgetID);
            _view.Filename = gadget.Filename.Substring(0, gadget.Filename.Length - 5); // Remove ".ascx"
        }
        
        public virtual void Validate()
        {
            if (_view.CurrentTab != "properties")
                return;
            if (_view.Filename.Trim() == "")
                AddValidationError(2, GetText(_view, "NoFilenameError"));
            string filename = _view.Filename;
            if (!filename.ToLower().EndsWith(".ascx"))
                filename += ".ascx";
        }
        
        public void GetPropertiesViewData(DashboardGadget gadget)
        {
            gadget.Filename = _view.Filename;
            if (!gadget.Filename.ToLower().EndsWith(".ascx"))
                gadget.Filename += ".ascx";
            gadget.Activated = true;
            gadget.CustomGadget = true;
        }

        public virtual void Save()
        {
            Validate();
            if (!IsValid)
                return;
            DashboardGadget gadget = _view.GadgetID == -1
                                         ? new DashboardGadget()
                                         : SystemRepository.GetDashboardGadget(_view.GadgetID);
            GetPropertiesViewData(gadget);
            if (_view.GadgetID == -1)
            {
                _view.Code = DiskAccess.ReadTextFile("Admin\\Resources\\NewDashboardGadgetCode.ascx");
                _view.Xml = DiskAccess.ReadTextFile("Admin\\Resources\\NewDashboardGadgetTranslation.xml");
            }
            gadget.SetCode(_view.Code, _administrator);
            gadget.SetXml(_view.Xml, _administrator);
            SystemRepository.SaveDashboardGadget(gadget);
            _view.SetGadgetID(gadget.ID);
            DiskAccess.CreateTextFile("Admin\\DashboardGadgets\\" + gadget.Filename, gadget.GetCode());
            DiskAccess.CreateTextFile(
                "Admin\\Translations\\DashboardGadgets\\" + gadget.GetFilenameWithoutExtension() + ".xml",
                gadget.GetXml());
        }
    }
}